<?php

class RestaurantesControl extends Restaurantes{

	public function insertRestaurantes($arr){
		try {
			$obj = new Restaurantes();
			foreach($arr as $chave => $valor){
				$chave = ucfirst($chave);
				$met = 'set'.$chave;
				$obj->$met($valor);
			}
			$db = new Db;
			$db->Insert('restaurantes',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function updateRestaurantes($arr){
		try {
			$obj = new Restaurantes();
			foreach($arr as $chave => $valor){
				$chave = ucfirst($chave);
				$met = 'set'.$chave;
				$obj->$met("'".$valor."'");
			}
			$db = new Db;
			$db->Update('restaurantes',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function deleteRestaurantes($id){
		try {
			$db = new Db;
			$db->Delete('restaurantes',$id);
			$this->lista = $db->res;
		} catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function consultarRestaurantes($parametros){
		try {
			$campos = array('*');
			$where = 'idrestaurantes = '.$parametros;
			$order = '';
			$db = new Db;
			$db->Select('restaurantes',$campos,$where,$order);
			$this->lista = json_encode($db->res[0]);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}	
	public function listarRestaurantes(){
		try {
			$campos = array('restaurantes.*, (SELECT IFNULL(AVG(avaliacoes.valor),0) FROM avaliacoes WHERE avaliacoes.pratos_idpratos = pratos.idpratos) AS points');
			$where = 'restaurantes.ativo = 1 AND pratos.ativo = 1 GROUP BY restaurantes.idrestaurantes';
			$order = 'restaurantes.nome ASC';
			$db = new Db;
			$db->Select('restaurantes 
			INNER JOIN pratos ON pratos.restaurantes_idrestaurantes = restaurantes.idrestaurantes 
			LEFT JOIN avaliacoes ON avaliacoes.pratos_idpratos = pratos.idpratos',$campos,$where,$order);			
			$this->lista = json_encode(Control::arrayUtf8_converter($db->res));
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}	
}
?>